/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionbean;

import entitybean.Artists;
import entitybean.Users;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.annotation.ejb.RemoteBinding;

/**
 *
 * @author Thien Nguyen
 */
@Stateless
@org.jboss.ejb3.annotation.RemoteBinding(jndiBinding = "UsersJNDI")
@org.jboss.ejb3.annotation.LocalBinding(jndiBinding = "UsersLocalJNDI")
public class UsersSessionBean implements UsersSessionBeanRemote, UsersSessionBeanLocal {

    @PersistenceContext(unitName = "J2EE_ShareMusic-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public boolean checkLogin(String username, String password) {
        String jpql = "SELECT a FROM Users a WHERE a.username = :uid and a.password = :pw ";
        Query query = em.createQuery(jpql);
        query.setParameter("uid", username);
        query.setParameter("pw", password);
        List result = query.getResultList();
        if (result.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean checkRole(String username) {
        Users user = em.find(Users.class, username);
        if (user != null) {
            return user.getIsAdmin();
        }
        return false;
    }

    public List loadUser() {
        String jpql = "SELECT a FROM Users a";
        javax.persistence.Query query = em.createQuery(jpql);
        List list = query.getResultList();
        return list;
    }

    public List getUser(String name) {
        String jpql = "SELECT a FROM Users a WHERE a.username =:name";
        javax.persistence.Query query = em.createQuery(jpql);
        query.setParameter("name", name);
        List list = query.getResultList();
        return list;
    }

    public boolean addUser(String username, String password, java.util.Date createdate, String email, boolean role, float upload) {
        String jpql = "SELECT a FROM Users a WHERE a.username =:name";
        javax.persistence.Query query = em.createQuery(jpql);
        query.setParameter("name", username);
        List list = query.getResultList();
        if (list.size() == 0) {
            Users newA = new Users(username, password, createdate, email, role, 0);
            persist(newA);
            return true;
        } else {
            return false;
        }
    }

    public boolean deleteUser(String username) {
        String jpql = "DELETE FROM Upload a WHERE A.users.username =:username";
        javax.persistence.Query query = em.createQuery(jpql);
        query.setParameter("username", username);
        int result = query.executeUpdate();
        Users user = em.find(Users.class, username);
        if (user != null) {
            em.remove(user);
            return true;
        }
        return false;
    }

    public boolean saveUser(String username, String password,String email, boolean role) {
        Users user = em.find(Users.class, username);
        if (user != null) {
            user.setPassword(password);
            user.setEmail(email);
            user.setIsAdmin(role);
            persist(user);
            return true;
        } 
        return false;
    }

    public boolean checkAvailableUser(String username) {
        Users user = em.find(Users.class, username);
        if (user != null) return true;
        else return false;
    }

    public boolean changePassword(String username, String password) {
        Users user = em.find(Users.class, username);
        if (user != null) {
            user.setPassword(password);
            persist(user);
            return true;
        }
        else return false;
    }

    public boolean checkUserPassword(String username, String password) {
        Users user = em.find(Users.class, username);
        if(user.getPassword().equals(password)) return true;
        else return false;
    }

    
}
